package com.pig4cloud.pig.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.service.CommonService;
import com.pig4cloud.pig.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author mobai
 */
@RestController
@RequiredArgsConstructor
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
@RequestMapping("/common")
public class CommonController {
	private final CommonService commonService;


	/**
	 * 获取显示字段列表
	 */
	@Operation(summary = "获取显示字段列表", description = "获取显示字段列表")
	@GetMapping("{label}/field")
	public R getMetaFieldList(@PathVariable String label) {
		return commonService.getMetaFieldList(label);
	}

	/**
	 * 分页查询
	 *
	 * @param page   分页对象
	 * @param params 参数
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@PostMapping("{label}/page")
	public R getMetaEntityPage(@ParameterObject Page page, @RequestBody Map<String, Object> params, @PathVariable String label) {
		return commonService.getMetaEntityPage(page, params, label);
	}
	/**
	 * 获取单条数据的详情
	 */
	@Operation(summary = "获取单条数据的详情", description = "获取单条数据的详情")
	@GetMapping("{label}/details/{id}")
	public R getDetails(@PathVariable String label,@PathVariable String id) {
		return commonService.getDetails(label,id);
	}

	/**
	 * 添加实体数据
	 */

	@Operation(summary = "添加实体数据", description = "添加实体数据")
	@PostMapping("{label}")
	public R add(@PathVariable String label, @RequestBody Map<String, Object> params) {
		return commonService.addTableData(label, params);
	}

	/**
	 * 编辑实体数据
	 */
	@Operation(summary = "编辑实体数据", description = "编辑实体数据")
	@PutMapping("{label}")
	public R updateById(@PathVariable String label, @RequestBody Map<String, Object> params) {
		return commonService.updateEntityById(label, params);
	}

	/**
	 * 删除实体数据
	 */
	@Operation(summary = "删除实体数据", description = "删除实体数据")
	@DeleteMapping("{label}")
	public R removeById(@PathVariable String label, @RequestBody Long[] ids) {
		return commonService.removeDataById(label, ids);
	}

	/**
	 * 根据实体label和数据id发起流程
	 */
	@Operation(summary = "根据实体label和数据id发起流程", description = "根据实体label和数据id发起流程")
	@PostMapping("{label}/startFlow/{id}")
	public R startFlow(@PathVariable String label, @PathVariable String id) {
		return commonService.startFlow(label, id);
	}
}
